const mixin = require('../../../mixins');

const {
	globalData
} = getApp();
Component({
	behaviors: [mixin],

	data: {
		currIndex: 0,
		cornerMarkerObj: {
			left: [],
			right: []
		},
		bottomHeight: 'calc( 100vh - 120rpx )',
		autoPlay:false,
		animePlaying: false
	},
	properties: {
		mf_data: Object,
	},
	pageLifetimes: {
		show: function () {
		  this.setData({
		    ['mf_data.autoPlay'] : true
		  })
		},
		hide: function () {
		  this.setData({
		    ['mf_data.autoPlay'] : false
		  })
		},
	},
	attached() {
		try {
		let stemInfoSync =wx.getSystemInfoSync()
		let pixelRatio1 = 750 / stemInfoSync.windowWidth;

		this.initCornerMarker();
		this.setData({
			// bottomHeight: globalData.isIpx ? 'calc( 100vh - 150rpx )' : 'calc( 100vh - 120rpx )',
			bottomHeight: globalData.isIpx ? stemInfoSync.screenHeight*pixelRatio1-150 :  stemInfoSync.screenHeight*pixelRatio1-120
		})
		this.triggerEvent('triggerComponent', {
			data: {
				trackData: this.properties.mf_data.children[0]?.children[0]?.trackingParams,
				type: "track"
			}
		}, {
			bubbles: true,
			composed: true
		});
		}catch(err){}
	},
	ready(){
		if(this.data.mf_data && this.data.mf_data.label1 == '首屏swiper'){
      this.data.autoPlay = this.data.mf_data.autoPlay || false
			this.setData({
				['mf_data.autoPlay'] : false
			})
			globalData.event.on('swiperAutoPlay',this.swiperAutoPlay,this)
		}
	},
	methods: {

		//注册的事件
		swiperAutoPlay(){
			if(this.data.autoPlay){
				this.setData({
				    ['mf_data.autoPlay'] : true
				  })
			}
		},
		stopTouchMove(){

			return false
		},

		initCornerMarker() {
			const {
				cornerMarker,
				children
			} = this.properties.mf_data;
			if (!cornerMarker) return;
			const {
				currIndex
			} = this.data;
			this.setData({
				cornerMarkerObj: {
					left: String(currIndex + 1).split(""),
					right: String(children.length).split(""),
				}
			})
		},
		onChange(event) {
			const {
				current,
				isChanging
			} = event.detail;
			if (!isChanging) {
				this.data.animePlaying = true;
				this.setData({
					currIndex: current
				}, () => {
					this.initCornerMarker();
				})
				this.triggerEvent('triggerComponent', {
					data: {
						trackData: this.properties.mf_data.children[current]?.children[0]?.trackingParams,
						type: "track"
					}
				}, {
					bubbles: true,
					composed: true
				});
			}
		},
		onAnimationFinish(e){
			// this.data.animePlaying = false;
		},
		changeTo(_index) {
			this.onChange({
				detail: {
					current: _index
				}
			})
		},
		clickEvent(e){
			const {changedTouches, currentTarget:{dataset} } =e
			if(changedTouches?.dataset?.eventlist ||dataset?.eventlist ) {
				let ev =  changedTouches?.dataset?.eventlist ||dataset?.eventlist
				if(ev.length) {
					wx.navigateTo({
						url: '/' + ev[0].href
					})
				}

			}
		},
		navigationRight() {
			let {
				currIndex
			} = this.data;
			let index = currIndex < (this.properties.mf_data.children.length - 1) ? currIndex + 1 : 0;
			this.changeTo(index)
		},
		navigationLeft() {
			let {
				currIndex
			} = this.data;
			let index = currIndex > 0 ? currIndex - 1 : this.properties.mf_data.children.length - 1;
			this.changeTo(index)
		},
		touchStart(e) {
			const {
				clientX,
				clientY
			} = e.changedTouches[0];
			// getCurrentPages()[0].$vm.setUtoolsP(false)
			this.startX = clientX;
			this.startY = clientY
		},
		touchMove(e) {
		},
		touchEnd(e) {
			if (Date.now() - this.startTime < this.data.duration) return; // 如果两次触发的时间小于动画时间的话，不进行再次触发
			const {
				clientX,
				clientY
			} = e.changedTouches[0];
			let y = clientY-this.startY
			let x = Math.abs(clientX - this.startX)
			let _y = Math.abs(y)
			if (_y < 20 ||  _y < x+15 ) {
				return
			}
			clearInterval(this.timer)
			this.timer = null
			this.startTime = Date.now(); // 当前动画触发的开始时间
			this.handleTouchEnd(y);
			const {
				autoPlay
			} = this.properties.mf_data;
			if (autoPlay && !this.timer) {
				setTimeout(() => {
					this.autoPlay()
				}, 500)
			}

		},
		OnCurrentChange(e) {
			const {
				detail: {
					currIndex
				}
			} = e
			this.setData({
				currIndex
			})
		},
		// 连滑开关
		timerTouch(time = 200) {
			this.switch = true;
			setTimeout(() => {
				this.switch = false;
			}, time)
		},
		// 处理滑动结束后的事情
		handleTouchEnd(result) {
			let {
				currIndex
			} = this.data;
			const {
				children = []
			} = this.properties.mf_data;
			let index = Number(currIndex);
			// 滑动滚动需要判断边界值
			if (result > 0) {
				if (index !== 0) {

					index = index - 1;
				}
			} else {
				if (index !== children.length - 1) {

					index = index + 1;
				}
			}
			this.setData({currIndex:  index})
			this.triggerEvent('triggerComponent', {
				data: {
					trackData: this.properties.mf_data.children[index]?.children[0]?.trackingParams,
					type: "view"
				}
			}, {
				bubbles: true,
				composed: true
			});
		},

	},
});
